package com.xm.xmapp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.LongReRentOrderActivity;
import com.xm.xmapp.MainActivity;
import com.xm.xmapp.R;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.RequestInterFaceCode.SeekCarCode;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseFragment;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.MainActivityInterFace;
import com.xm.xmapp.request.bean.Get_car_statusRequest;
import com.xm.xmapp.request.bean.Handle_car_doorRequest;
import com.xm.xmapp.request.bean.Seek_carRequest;
import com.xm.xmapp.request.bean.Seek_carResponse;
import com.xm.xmapp.response.bean.CustomHeightBean;
import com.xm.xmapp.response.bean.InterFaceLongRentOrderAble;
import com.xm.xmapp.response.bean.LongRentOrderBean;
import com.xm.xmapp.response.bean.ResponseCarStatus;
import com.xm.xmapp.tools.Tools;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.xm.xmapp.R.id.chexing;
import static com.xm.xmapp.R.id.close;
import static com.xm.xmapp.R.id.go_long_rent;
import static com.xm.xmapp.R.id.long_rent_alert;
import static com.xm.xmapp.R.id.open;
import static com.xm.xmapp.R.id.seat;
import static com.xm.xmapp.R.id.statusImg;
import static com.xm.xmapp.R.id.text3;

public class LongTimeRentCarFragment extends BaseFragment implements OnClickListener {

    TextView longRentAlert;
    TextView goLongRent;
    ImageView cha;
    RelativeLayout top;
    TextView tvCarNumber;
    TextView tvCarGenreName;
    TextView tvCarSeat;
    TextView tvEndurance;
    LinearLayout llEndurance;
    RelativeLayout rlFindCar;
    RelativeLayout rlCloseCar;
    RelativeLayout rlOpenCar;
    LinearLayout bottom;
    LinearLayout layout;
    private LongRentOrderBean bean;

    /**
     * type = 1表示可以请求接口，打开车门 type = 0表示车门已开，可以关闭车门 默认是1
     */
    private int type = 1;
    private boolean isOperateCar;

    public LongTimeRentCarFragment() {
        super();

    }

    public static LongTimeRentCarFragment newInstance(InterFaceLongRentOrderAble bean) {
        LongTimeRentCarFragment test = new LongTimeRentCarFragment();
        test.setArguments(new Bundle());
        test.setData(bean);
        return test;
    }

    private void setData(InterFaceLongRentOrderAble bean) {
        this.bean = (LongRentOrderBean) bean;
    }

    @Override
    public int setContentView() {
        return R.layout.fragment_longrent_order;
    }

    public InterFaceLongRentOrderAble getBean() {
        return bean;
    }

    public void setBean(InterFaceLongRentOrderAble bean) {
        if (bean == null)
            return;
        this.bean = (LongRentOrderBean) bean;
        if (view != null) {
            initData();
            heightbean = new CustomHeightBean();
            heightbean.ExitOffset = layout.getHeight();
            heightbean.MaxOffset = bottom.getHeight();
            heightbean.flag = MainActivityInterFace.INIT_ORDER_STATUS;
            if (heightbean.ExitOffset == 0) {
                heightbean.ExitOffset = (int) getResources().getDimension(R.dimen.dp216);
                heightbean.MaxOffset = (int) getResources().getDimension(R.dimen.dp80);
            }
            MyAppcation.getMyAppcation().notifyDataSetChanged(MainActivity.class, Type.A_Main.CHANGE_UI_CODE,
                    heightbean);

        }
    }
    boolean isSearch = true;

    @Override
    public void initView() {

        layout = (LinearLayout) findViewById(R.id.layout);
        tvCarNumber = (TextView) findViewById(R.id.tv_car_number);
        tvEndurance = (TextView) findViewById(R.id.tv_endurance);
        rlFindCar = (RelativeLayout) findViewById(R.id.rl_find_car);
        tvCarGenreName = (TextView) findViewById(R.id.tv_car_genre_name);
        tvCarSeat = (TextView) findViewById(R.id.tv_car_seat);
        rlCloseCar = (RelativeLayout) findViewById(R.id.rl_close_car);
        rlOpenCar = (RelativeLayout) findViewById(R.id.rl_open_car);
        llEndurance = (LinearLayout) findViewById(R.id.ll_endurance);
        bottom = (LinearLayout) findViewById(R.id.bottom);
        top = (RelativeLayout) findViewById(R.id.top);
        cha = (ImageView) findViewById(R.id.cha);
        longRentAlert = (TextView) findViewById(R.id.long_rent_alert);
        goLongRent = (TextView) findViewById(go_long_rent);

    }

    @Override
    public void initData() {

        tvCarNumber.setText(bean.car_number);
        tvCarGenreName.setText(bean.car_genre_name);
        tvCarSeat.setText(bean.car_seat + "座");
        tvEndurance.setText(bean.getEndurance() + "km");

        if (bean.long_rent_alert == 1) {
            top.setVisibility(View.VISIBLE);
            if (bean.long_rent_alert_days > 0) {
                longRentAlert.setText("您好，您当前的合约期已不足" + bean.long_rent_alert_days + "天。");
            } else if (bean.long_rent_alert_days == 0) {
                longRentAlert.setText("您好，当前合约期已到期,");
            } else {
                longRentAlert.setText("您好，当前合约期已过期" + bean.long_rent_alert_days + "天。");
            }
        } else {
            top.setVisibility(View.GONE);
        }

//        if (bean.is_support_car_door == 1) {
//            open.setVisibility(View.VISIBLE);
//            close.setVisibility(View.VISIBLE);
//        } else {
//            open.setVisibility(View.INVISIBLE);
//            close.setVisibility(View.INVISIBLE);
//        }
    }

    private CustomHeightBean heightbean;

    @Override
    public void ViewTreeObserver() {
        super.ViewTreeObserver();
        heightbean = new CustomHeightBean();
        heightbean.flag = MainActivityInterFace.INIT_ORDER_STATUS;
        heightbean.ExitOffset = layout.getHeight();
        heightbean.MaxOffset = bottom.getHeight();
        MyAppcation.getMyAppcation().notifyDataSetChanged(MainActivity.class, Type.A_Main.CHANGE_UI_CODE, heightbean);
    }

    @Override
    public void initEvent() {

//        TextView longRentAlert;
//        TextView goLongRent;
//        ImageView cha;
//        RelativeLayout top;
//        TextView tvCarNumber;
//        TextView tvCarGenreName;
//        TextView tvCarSeat;
//        TextView tvEndurance;
//        LinearLayout llEndurance;
//        RelativeLayout rlFindCar;
//        RelativeLayout rlCloseCar;
//        RelativeLayout rlOpenCar;
//        LinearLayout bottom;
//        LinearLayout layout;


        rlFindCar.setOnClickListener(this);
        rlCloseCar.setOnClickListener(this);
        rlOpenCar.setOnClickListener(this);
        longRentAlert.setOnClickListener(this);
        goLongRent.setOnClickListener(this);
        layout.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        bottom.setOnClickListener(this);
        cha.setOnClickListener(this);


    }

    private void get_car_status() {
        Get_car_statusRequest requestBean = new Get_car_statusRequest();
        requestBean.car_id = bean.car_id;
        requestBean.type = 1;
        promptDialog.show();
        MyAppcation.getMyAppcation().getPostData(getActivity(), requestBean, new GetDataInterFace<String>() {

            @Override
            public void onResponse(String response) {

                promptDialog.dismiss();
                ResponseCarStatus bean = JSON.parseObject(response, ResponseCarStatus.class);
                tvEndurance.setText(bean.getEndurance() + "km");

            }
        }, new DisssmissInterFace() {

            @Override
            public void putError(VolleyError error) {
                promptDialog.dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.cha:
                top.setVisibility(View.GONE);
                break;

            case R.id.go_long_rent:
                if (bean != null) {
                    Intent intent = new Intent(MyAppcation.getMyAppcation(), LongReRentOrderActivity.class);
                    intent.putExtra("order_id", bean.order_id);
                    startActivity(intent);
                }
                break;
            case R.id.bottom:
                get_car_status();
                break;

            case R.id.rl_find_car:
                if (isSearch) {
                    isSearch = false;
                    seek_carRequest();
                } else {
                    Tools.showMessage("操作频繁，稍后重试");
                }
                break;
            case R.id.rl_open_car:
                if (isOperateCar) {
                    isOperateCar = false;
                    type = 1;
                    operateCar();
                } else {
                    Tools.showMessage("操作频繁，稍后重试");
                }
                break;

            case R.id.rl_close_car:
                if (isOperateCar) {
                    isOperateCar = false;
                    type = 0;
                    operateCar();
                } else {
                    Tools.showMessage("操作频繁，稍后重试");
                }
                break;

            default:
                break;
        }

    }

    private void seek_carRequest() {
        Seek_carRequest requestBean = new Seek_carRequest();
        requestBean.car_id = bean.car_id;
        requestBean.type = SeekCarCode.TYPE;
        promptDialog.show();
        MyAppcation.getMyAppcation().getPostData(getActivity(), requestBean, new GetDataInterFace<String>() {

            @Override
            public void onResponse(String response) {
                promptDialog.dismiss();
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        isSearch = true;
                    }
                }, 5000);
                Seek_carResponse bean = JSON.parseObject(response, Seek_carResponse.class);
                bean.type = 1;
                MyAppcation.getMyAppcation().notifyDataSetChanged(MainActivity.class, Type.A_Main.SEARCH_CAR_CODE,
                        bean);

            }
        }, new DisssmissInterFace() {

            @Override
            public void putError(VolleyError error) {
                promptDialog.dismiss();
                isSearch = true;
                Tools.showMessage("操作失败");
            }
        });

    }

    /**
     * 开关车门寻车
     */
    private void operateCar() {

        Handle_car_doorRequest requestBean = new Handle_car_doorRequest();
        requestBean.car_id = bean.car_id;
        requestBean.type = type;
        promptDialog.show();
        MyAppcation.getMyAppcation().getPostData(getActivity(), requestBean, new GetDataInterFace<String>() {

            @Override
            public void onResponse(String response) {
                promptDialog.dismiss();
                try {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            switch (type) {
                                case 0:
                                case 1:
                                    isOperateCar = true;
                                    break;
                                case 2:
                                    isSearch = true;
                                    break;
                                default:
                                    break;
                            }
                        }
                    }, 5000);
                    if (response.equals("true")) {

                        switch (type) {
                            case 0:

                                Tools.showMessage("关门成功");
                                break;
                            case 1:
                                Tools.showMessage("开门成功");
                                break;
                            case 2:
                                Tools.showMessage("操作成功");
                                break;
                            default:
                                break;
                        }

                    } else {
                        Tools.showMessage("操作失败");
                    }
                } catch (Exception e) {
                    Tools.showMessage("操作失败");
                }
            }
        }, new DisssmissInterFace() {

            @Override
            public void putError(VolleyError error) {

                switch (type) {
                    case 0:
                        isOperateCar = true;
                        Tools.showMessage("关门失败");
                        break;
                    case 1:
                        isOperateCar = true;
                        Tools.showMessage("开门失败");
                        break;
                    case 2:
                        isSearch = true;
                        Tools.showMessage("操作失败");
                        break;
                    default:
                        break;
                }
                promptDialog.dismiss();
            }
        });

    }


}
